package com.agent.controller;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.agent.exception.MyException;
import com.agent.model.Agent;
import com.agent.model.AgentOrderApply;
import com.agent.service.IAgentOrderApplySer;

@Controller
@RequestMapping(value="/agentOrderApply",produces="text/html;charset=UTF-8")
public class AgentOrderApplyController extends BaseController{
	@Autowired
	IAgentOrderApplySer agentOrderApplySer;
	
	/**
	 * 代理提交审核
	 * @param model
	 * @param request
	 * @param agentOrderApply
	 * @return
	 */
	@RequestMapping("/addAgentOrderApply")
	@ResponseBody
	public String addAgentOrderApply(Model model, HttpServletRequest request,AgentOrderApply agentOrderApply){
		try {
			int result = agentOrderApplySer.addAgentOrderApply(agentOrderApply);
			return super.ajaxReturnTool.success(result).toJson();
		} catch (MyException e) {
			return super.ajaxReturnTool.fail(e.getCode()).toJson();
		}
	}
	
	/**
	 * 展示提交审核信息
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/findListAgentOrderApply")
	@ResponseBody
	public String findListAgentOrderApply(Model model, HttpServletRequest request){
		Agent agent = (Agent) request.getSession().getAttribute("agent");
		Integer level =agent.getLevel();
		Integer agentid= agent.getAgentid();
		try {
			List<AgentOrderApply> resultList = agentOrderApplySer.findListAgentOrderApply(level,agentid);
			return super.ajaxReturnTool.success(resultList).toJson();
		} catch (MyException e) {
			return super.ajaxReturnTool.fail(e.getCode()).toJson();
		}
	}
	
	/**
	 * 管理员通过审核
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/agentOrderApplyAdopt")
	@ResponseBody
	public String agentOrderApplyAdopt(Model model, HttpServletRequest request,AgentOrderApply agentOrderApply){
		try {
			int result = agentOrderApplySer.agentOrderApplyAdopt(agentOrderApply);
			return super.ajaxReturnTool.success(result).toJson();
		} catch (MyException e) {
			return super.ajaxReturnTool.fail(e.getCode()).toJson();
		}
	}
	
	/**
	 * 审核未通过
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/agentOrderApplyNotPass")
	@ResponseBody
	public String agentOrderApplyNotPass(Model model, HttpServletRequest request,AgentOrderApply agentOrderApply){
		try {
			int result = agentOrderApplySer.agentOrderApplyNotPass(agentOrderApply);
			return super.ajaxReturnTool.success(result).toJson();
		} catch (MyException e) {
			return super.ajaxReturnTool.fail(e.getCode()).toJson();
		}
	}
	
	/**
	 * 上传图片
	 * @param request
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("/uploadImg")
	@ResponseBody
	public String uploadImg(HttpServletRequest request)
			throws IllegalStateException, IOException {
		String localPath="";
		CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		if (resolver.isMultipart(request)) {
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) (request);
			LinkedMultiValueMap<String,MultipartFile> multiFileMap = (LinkedMultiValueMap<String, MultipartFile>) multiRequest.getMultiFileMap();
			Collection<List<MultipartFile>> values = multiFileMap.values();
			Iterator<List<MultipartFile>> iterator = values.iterator();
			List<MultipartFile> next = iterator.next();
			MultipartFile file2 = next.get(0);
			if(!file2.isEmpty()){
				String fileName = file2.getOriginalFilename();
				String uuidName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
//				localPath = "/HaiYou/images/fytravel" + uuidName;
				localPath = "E:/temp/"+ uuidName;
				File newFile = new File(localPath);
				file2.transferTo(newFile);
			}
	}
		return super.ajaxReturnTool.success(localPath).toJson();
	}
	
	
	
}
